<?php
/**
 * Created by PhpStorm.
 * User: 程序猴
 * Date: 2018/5/10
 *  Q Q: 505413
 * Time: 14:13
 */

namespace app\admin\controller;
use app\admin\controller\Base;
use think\Db;
use think\Data;
class Goods extends Base
{
    //商品列表
    public function goodsList()
    {
        $key = input('key');
        $map = [];
        if($key&&$key!=="")
        {
            $map['goods_name'] = ['like',"%" . $key . "%"];
        }
        //商品列表
        $list = Db::name('goods')->order('goods_id desc')->where($map)->paginate(10);
        //商品分类
        $cate = Db::name('category')->select();
        $cate = Data::tree($cate,'name','id','pid');
        $this->assign('list',$list);
        $this->assign('cate',$cate);
        return $this->fetch();
    }
    //是否上下架
    public function isOnSale()
    {
        $goods_id = input('param.id');
        $status = Db::name('goods')->where('goods_id',$goods_id)->value('is_on_sale');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('goods')->where('goods_id',$goods_id)->setField(['is_on_sale'=>0]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已下架']);
        }
        else
        {
            $flag = Db::name('goods')->where('goods_id',$goods_id)->setField(['is_on_sale'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已上架']);
        }
    }
    //拖拽图片上传
    public function uploadPic()
    {
        $file = request()->file('file');
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/goods');//指定上传路径
            $name = $info->getSaveName(); // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
            if(false === $info){
                return json(['code' => -1, 'url' => 'goodsList/index', 'msg' => '上传失败，请重试']);
            }else{
                return json(['code' => 1, 'url' => 'goodsList/index', 'msg' => '上传成功','name' => $name]);
            }
        }
    }
    //添加商品
    public function goodsAdd()
    {
        if(request()->isAjax()){
            $param = input('post.');
            if(!empty($param['is_on_sale'])){
                if($param['is_on_sale'] == 'on') {
                    $param['is_on_sale'] = '1';
                }
            }else{
                $param['is_on_sale'] = '0';
            }
            $param['create_time'] = time();
            $param['update_time'] = time();
            unset($param['file']);
            $res = Db::name('goods')->insert($param);
            if($res){
                return json(['code' => 1, 'url' => 'reduction/index', 'msg' => '添加商品成功']);
            }else{
                return json(['code' => -1, 'url' => 'reduction/index', 'msg' => '添加商品失败，请重试']);
            }
        }
        //树形分类
        $list = Db::name('category')->select();
        $list = Data::tree($list,'name','id','pid');
        $this->assign('list',$list);
        return $this->fetch();
    }
    //编辑商品
    public function goodsEdit()
    {
        $goods_id = input('param.goods_id');
        if(request()->isAjax()){
            $param = input('post.');
            if(!empty($param['is_on_sale'])){
                if($param['is_on_sale'] == 'on') {
                    $param['is_on_sale'] = '1';
                }
            }else{
                $param['is_on_sale'] = '0';
            }
            $param['update_time'] = time();
            unset($param['file']);
            $data = Db::name("goods")->where(array('goods_id'=>$goods_id))->update($param);
            if(false === $data){
                return json(['code' => -1, 'url' => 'goods/goodsList', 'msg' => '修改商品失败']);
            }else{
                return json(['code' => 1, 'url' => 'goods/goodsList', 'msg' => '修改商品成功']);
            }
        }
        $goods = Db::name('goods')->where(array('goods_id'=>$goods_id))->find();
        //树形分类
        $cate = Db::name('category')->select();
        $cate = Data::tree($cate,'name','id','pid');
        $this->assign('goods',$goods);
        $this->assign('cate',$cate);
        return $this->fetch();
    }
    //删除商品
    public function goodsDel()
    {
        $goods_id = input('goods_id');
        $res = Db::name('goods')->where(array('goods_id'=>$goods_id))->delete();
        if($res){
            return json(['code' => 1, 'url' => 'reduction/index', 'msg' => '删除商品成功']);
        }else{
            return json(['code' => -1, 'url' => 'reduction/index', 'msg' => '删除商品失败，请重试']);
        }
    }
    //批量删除
    public function aDel() {
        $ids = input('post.ids/a');
        $data = Db::name('goods')->delete($ids);
        if(false === $data){
            return json(['code' => -1, 'url' => '/goods/goodsList', 'msg' => '批量删除商品失败','ids'=>$ids]);
        }else{

            return json(['code' => 1, 'url' => '/admin/goods/goodsList', 'msg' => '批量删除商品成功']);
        }
    }
}